from django.db import models
from goods.models import Product
# Create your models here.
"""
sku模型设计思路
    一个商品对应多个sku
    颜色表单拆出来，和sku多对多
    尺码表单拆出来，和sku多对多
    规格表单拆出来，和sku多对多
    库存表拆出来，和sku一对多，因为一个sku表对应多个库存，但是这个库存需要根据颜色和尺码来决定
        库存表中，需要有颜色，尺码，规格，库存数
        这样做的目的可以占用更少的内存空间，库存根据不同的颜色和尺码和规格来决定
        
    
"""
class Sku(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    # PositiveIntegerField只能存储正整数，不能存储负数或0数字
    stock = models.PositiveIntegerField(default=0,verbose_name='库存')
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0,verbose_name="价格")
    image=models.ImageField(upload_to="media/sku",null=True,blank=True)
    attribute = models.ManyToManyField(to='attribute', through='skuAndAttribute', through_fields=('sku', 'attribute'))
    freight=models.DecimalField(max_digits=10, decimal_places=2, default=0,verbose_name="运费")
    original_price=models.DecimalField(max_digits=10, decimal_places=2, default=0,verbose_name="原价")
    frozen_stock = models.PositiveIntegerField(default=0,verbose_name='冻结库存')# 冻结库存
    def __str__(self):
        return self.product.name
    class Meta:
        verbose_name = "sku"
        verbose_name_plural = "sku"
class skuAndAttribute(models.Model):
    sku = models.ForeignKey(to='Sku', on_delete=models.CASCADE)
    attribute = models.ForeignKey(to='attribute', on_delete=models.CASCADE)
    def __str__(self):
        return self.sku.product.name
    class Meta:
        verbose_name = "sku所拥有的属性"
class attribute(models.Model):
    name = models.CharField(max_length=100,verbose_name="属性名称")
    value = models.CharField(max_length=100,verbose_name="属性值")
    def __str__(self):
        return self.name+" "+self.value
    class Meta:
        verbose_name = "属性"
        verbose_name_plural = verbose_name




